#!/bin/sh
#
# Init file for starting apache processes serving webdav data (webdav)
#
# chkconfig: 2345 86 14
# description: WebDAV user processes
#

# Source function library
. /etc/rc.d/init.d/functions

# Source networking configuration.
[ -r /etc/sysconfig/network ] && . /etc/sysconfig/network

RETVAL=0
prog="webdav"

[ "${NETWORKING}" = "no" ] && exit 1

checkrunning() {
  vhost="$1"
  if [ ! -f "/var/webdav/$vhost/run/httpd-webdav.pid" ]; then
    return 1
  fi
  pid=$(cat "/var/webdav/$vhost/run/httpd-webdav.pid")
  kill -0 "$pid" >/dev/null 2>&1 || return 1
  return 0
}

startone() {
  vhost="$1"
  /usr/sbin/httpd -d "/var/webdav/$vhost" || return 1
}

restartone() {
  vhost="$1"
  /usr/sbin/apachectl -d "/var/webdav/$vhost" -k restart || return 1
}

stopone() {
  vhost="$1"
  /usr/sbin/apachectl -d "/var/webdav/$vhost" -k stop
}

stopall() {
  grep "^[1-9]" < /etc/webdav/shares.conf | while read port vhost; do
    if checkrunning "$vhost"; then
      echo -n "    ${vhost}..."
	  if stopone "$vhost"; then
	    echo " STOPPED"
	  else
	    echo " FAIL"
	  fi
	fi
  done
}

restartall() {
  grep "^[0-9]" < /etc/webdav/shares.conf | while read port vhost; do
    if checkrunning "$vhost"; then
      echo -n "    ${vhost}:${port}..."
      if [ "$port" = "0" ]; then
        if stopone "$vhost" ]; then
          echo " STOPPED"
        else
          echo " FAIL"
        fi
      else
        if restartone "$vhost"; then
          echo " RESTARTED"
        else
          echo " FAIL"
        fi
      fi
    else
      if [ ! "$port" = "0" ]; then
        echo -n "    ${vhost}:${port}..."
        if startone "$vhost"; then
          echo " STARTED"
        else
          echo " FAIL"
        fi
      fi
    fi
  done
}


startall() {
  grep "^[1-9]" < /etc/webdav/shares.conf | while read port vhost; do
    checkrunning "$vhost" || {
      echo -n "    ${vhost}..."
      if startone "$vhost"; then
        echo " STARTED"
      else
        echo " FAIL"
      fi
    }
  done
}

DAEMON=/usr/sbin/httpd

test -x $DAEMON || exit 0

start() {
	echo "Starting WebDAV services"
	startall
}

stop() {
	echo "Stopping WebDAV services"
	stopall
}

restart() {
	echo "Reloading WebDAV services"
	restartall
}

# See how we were called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                restart
                ;;
        reload)
        		restart
        		;;
        *)
                        echo $"Usage: $0 {start|stop|restart}"
                        exit 1
esac
exit $?
